package com.app.piemoment.view;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Typeface;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.app.piemoment.util.Utils;

public class ClipView extends View 
{
	private Paint mPaint = null;
	private float mRound = 0;
	
	
	
	private Drawable mShapeMask = null;
	
	
	
	
	public ClipView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}


	public ClipView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ClipView(Context context) {
		super(context);
		init();
	}
	

	private void init() {
		mPaint = new Paint();
		mPaint.setColor(Color.WHITE);
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setTypeface(Typeface.DEFAULT_BOLD);
		mPaint.setAntiAlias(true);
		mPaint.setStrokeWidth(0);
	}

	public ClipView(Context context, int round, int maskShapeRes)
	{
		super(context);
		
		mPaint = new Paint();
		mPaint.setColor(Color.WHITE);
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setTypeface(Typeface.DEFAULT_BOLD);
		mPaint.setAntiAlias(true);
		mPaint.setStrokeWidth(0);
		
		mRound =  Utils.dipToPixels(getContext(), round);
		
		/*if (imagePath == null)
		{
			InputStream is = getContext().getResources().openRawResource(R.drawable.default_icon);
			mBitmap = BitmapFactory.decodeStream(is);
		}
		else
		{
			File file = new File(imagePath);
			if (file.exists())
			{
				//BitmapFactory.Options options=new BitmapFactory.Options(); 
				//options.inSampleSize = 4;
				
				mBitmap = BitmapFactory.decodeFile(imagePath, null);
			}
			else
			{
				InputStream is = getContext().getResources().openRawResource(R.drawable.default_icon);
				mBitmap = BitmapFactory.decodeStream(is);
			}
		}*/
		
		
		mShapeMask = getResources().getDrawable(maskShapeRes);
	}
	
	@Override
	public void draw(Canvas canvas)
	{
		super.draw(canvas);
		Bitmap mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
		
		canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
		
		RectF backgroundRoundRect = new RectF(0, 0, getWidth(), getHeight());
		
	
		Path path = new Path();
		path.addCircle(getWidth()/2, getHeight()/2, getHeight()/2, Path.Direction.CCW);

		canvas.clipPath(path, Region.Op.REPLACE);

		int srcLeft = (mBitmap.getWidth()-getWidth())/2;
		int srcRight = (mBitmap.getHeight()-getHeight())/2;
		Rect srcRect = new Rect(srcLeft, srcRight, srcLeft+getWidth(), srcRight+getHeight()); 
		canvas.drawBitmap(mBitmap, srcRect, backgroundRoundRect, mPaint);
		
	}
}
